var app=angular.module('myApp', []).
  config(['$routeProvider', function($routeProvider) {
  $routeProvider.
      when('/', {templateUrl: 'view_tran_mis.html', controller: ListtranCtrl}).
     otherwise({redirectTo: '/'});
}]);

function ListtranCtrl($scope, $http,$window) {
    
    $scope.currentPage = 0;
    $scope.pageSize = 75;
	$scope.Math = window.Math;
	$scope.custarr = [];
	$scope.traninvalidmast = {};	
	$scope.search2 = {};	
  
	var d=new Date();	
	$scope.fromdt=d.getFullYear()+'-'+("0" + (d.getMonth()+1)).slice(-2)+"-"+ ("0" + d.getDate()).slice(-2);
	$scope.todt=d.getFullYear()+'-'+("0" + (d.getMonth()+1)).slice(-2)+"-"+ ("0" + d.getDate()).slice(-2);	
        
    $http.get('view_tran_mis_data.php?q=2&qfrom='+$scope.fromdt+'&qto='+$scope.todt).success(function(data) {
		$scope.traninvalidmast = data;		
		for(var i=0;i<data.length;i++){
			var item = data[i].Dist_Name;
			if ($scope.custarr.indexOf(item) == -1) 
				$scope.custarr.push(item);			
		}
		$scope.custarr.sort();				
	});
    $scope.numberOfPages=function()
    {
        return Math.ceil($scope.data.length / $scope.pageSize);                
    };
    				
	
	 $scope.total_db = function() {	 
        var total = 0;        
		var exist = false;
		 angular.forEach($scope.traninvalidmast, function(tran) {			
         	if(tran.Dist_Name == $scope.search2.Dist_Name || $scope.search2.Dist_Name == null || $scope.search2.Dist_Name == ''){
				exist =  true;
				total += +tran.Dr_Amount;
			}			
        });        
        return total;
	} 
	
	 $scope.total_cr = function() {
        var total = 0;
		var exist = false;
		 angular.forEach($scope.traninvalidmast, function(tran) {
         	if(tran.Dist_Name == $scope.search2.Dist_Name || $scope.search2.Dist_Name == null || $scope.search2.Dist_Name == ''){
				exist =  true;
				total += +tran.Cr_Amount;
			}
        });       
        return total;
	} 
	
	$scope.dfilter = function(from,to) 
	{				
		//alert(from+":"+to);
		if(!from && !to)
			q = '1';
		else{
			if(!to)
				to = '2050-01-01';
			if(!from)
				from = '1980-01-01';
			var q = '2&qfrom='+from+'&qto='+to;
		}

		$scope.custarr=[];
		$('button[data-loading-text]').button('loading');
		setTimeout(function () {
		   $http.get('view_tran_mis_data.php?q='+q).success(function(data) {
			$scope.traninvalidmast = data;	
			for(var i=0;i<data.length;i++){
				var item = data[i].Dist_Name;
				if ($scope.custarr.indexOf(item) == -1) 
					$scope.custarr.push(item);			
			}
			$scope.custarr.sort();
			$('button[data-loading-text]').button('reset');
			});
		}, 2000);

		/*$http.get('view_tran_mis_data.php?q='+q).success(function(data) {
			$scope.traninvalidmast = data;		
			for(var i=0;i<data.length;i++){
			var item = data[i].Dist_Name;
			if ($scope.custarr.indexOf(item) == -1) 
				$scope.custarr.push(item);			
			}
			$scope.custarr.sort();
		});
		
		$('button[data-loading-text]').on('click', function () {
			var btn = $(this);
			btn.button('loading');
			setTimeout(function () {
			   $http.get('view_tran_mis_data.php?q='+q).success(function(data) {
				$scope.traninvalidmast = data;		
				//alert('view_tran_mis_data.php?q='+q);
				btn.button('reset')
			});
			}, 2000)
		});*/
		
		/*alert(markers=$scope.traninvalidmast2);
		var retMarkers = [];		
		var j=parseInt(0);
		
		if(!from)
			from='0000-00-00';
		
		if(!to)
		{
			var d = new Date();					
			var d2 = d.getFullYear()+"-"+(parseInt(d.getMonth())+1)+"-"+d.getDate();			
			to=d2;
		}
		//alert(from+":"+to+":"+markers.length);
		//alert(from<markers[0].Date);
        for(var i = 0, len = markers.length; i < len; i++) 
		{
			var singleMarker = markers[i];
			//alert(singleMarker.Date >= from && singleMarker.Date <= to);
			if(singleMarker.Date >= from && singleMarker.Date <= to) 
			{			
				//retMarkers.push(singleMarker);
				retMarkers[j]=singleMarker;
				//alert(retMarkers[j].Referenceno);   
				j++;				    
			}
		}
		//alert($scope.traninvalidmast.length);
		$scope.traninvalidmast = retMarkers;
		$scope.traninvalidmast.length = j;*/
		
	}
/* 
	$scope.cancel = function() 
	{
		//location.reload(true);
		$scope.traninvalidmast=$scope.traninvalidmast2;
	}
    */
}
	
	/* 
	
	$scope.count = 0;
	$scope.$watch('traninvalidmast', function() {
	var cartTotal = 0;
	$scope.traninvalidmast.forEach(function(tran) {
      cartTotal += tran.total();
    });
   
   $scope.count += cartTotal;
	  //  if (traninvalidmast.length > 0) 
	
		//$window.alert(traninvalidmast.length);
	//for(count=0;count<$scope.traninvalidmast.length;count++)
		/* {
	//		$scope.count += count;	
			 total += $scope.traninvalidmast[count].netrate-$scope.traninvalidmast[count].Dr_Amount;
			 $scope.count += 1
			} */ 
	     // return total; 
		 
   // }, true);
 
	/* 
	$scope.$watch('traninvalidmast', function() {
    var cartTotal = 0;
    
    $scope.traninvalidmast.forEach(function(tran) {
      cartTotal += tran.total();
    });
    
    $scope.cartTotal = cartTotal;
  }, true); */
	
	/* 
	  $scope.totalP = function(){
			var total = 0;
			// $window.alert(traninvalidmast.length);
									 
			for(count=0;count<$scope.traninvalidmast.length;count++)
			{
				total += $scope.traninvalidmast[count].netrate-$scope.traninvalidmast[count].Dr_Amount;
			}
			return total;
		} */

		/* $scope.total = function() {
        var total = 0;
        angular.forEach($scope.traninvalidmast, function(tran) {
         	total += tran.netrate - tran.Dr_Amount;
        })

        return total;
	    }  */
     
	  
	  /* $scope.totalLength = function() {
        return $scope.traninvalidmast.reduce(function(current, tran) { return current + tran.length; }, 0);
    }; */

	  
	  
	 

    app.directive('capitalize', function () {
        return {
            require: 'ngModel',
            link: function (scope, element, attrs, modelCtrl) {
                var capitalize = function (inputValue) {
                    if (angular.isUndefined(inputValue))
                        return;
 
                    var capitalized = inputValue.toUpperCase();
                    if (capitalized !== inputValue) {
                        modelCtrl.$setViewValue(capitalized);
                        modelCtrl.$render();
                    }
                    return capitalized;
                };
                modelCtrl.$parsers.push(capitalize);
                capitalize(scope[attrs.ngModel]);  // capitalize initial value
            }
        };
    });
    
	
//We already have a limitTo filter built-in to angular,
//let's make a startFrom filter
app.filter('startFrom', function() {
    return function(input, start) {
        start = +start; //parse to int
        return input.slice(start);
    };
});
app.filter('exact', function(){
  return function(items, match){
    var matching = [], matches, falsely = true;
	angular.forEach(match, function(value, key){
      falsely = falsely && !value;
    });
    if(falsely){
      return items;
    }
    angular.forEach(items, function(item){ // e.g. { title: "ball" }
      matches = true;
      angular.forEach(match, function(value, key){ // e.g. 'all', 'title'
        if(!!value) // do not compare if value is empty
          matches = matches && (item[key] === value);          
      });
      if(matches)
        matching.push(item);        
    });
    return matching;
  }
});
